﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Bronze.XXTong.DAL.DatabaseSpecific;
using Bronze.XXTong.DAL.Linq;
using Bronze.XXTong.DAL.EntityClasses;
using System.Data;

namespace Bronze.XXT.BLL
{
    /// <summary>
    /// 关于签到的操作
    /// </summary>
    public static class ReceiveICCard
    {
        /// <summary>
        /// 保存刷卡记录
        /// </summary>
        /// <param name="strICCardNum">卡号</param>
        /// <param name="strCardTime">刷卡时间</param>
        /// <param name="strPhoneSerialNumber">设备ID</param>
        /// <returns></returns>
        public static int SaveReceiveICCard(string strICCardNum,
                                            string strCardTime,
                                            string strPhoneSerialNumber,
                                            ref BLL.CheckAttendMsg attendMsg,
                                            ref string strGroupName)
        {
            int result = 0;
            string strUserName = string.Empty;
            string strOrgID = string.Empty;
            Guid deptID = Guid.Empty;
            using (DataAccessAdapter adapter = new DataAccessAdapter())
            {
                LinqMetaData metaData = new LinqMetaData(adapter);

                var stu = metaData.Student.SingleOrDefault(o => o.FIcCardNumber == strICCardNum);
                if (stu != null)
                {
                    result = 1;
                    strOrgID = stu.FOrgID;
                    strUserName = stu.FName;
                    deptID = stu.FClassID;
                }
                else
                {
                    var user = metaData.UserInfo.SingleOrDefault(o => o.FIcCardNumber == strICCardNum);
                    if (user != null)
                    {
                        result = 1;
                        strOrgID = user.FOrgID;
                        strUserName = user.FRealName;
                        deptID = user.FDepartmentID;
                    }
                }

                if (result == 1)
                {

                    string strSqlCommand = string.Format(@"INSERT INTO dbo.xxt_RecivedCard
                                        ( FOrgId ,
                                          FCardId ,
                                          FCardTime ,
                                          FDeviceId ,
                                          FInputTime
                                        )
                                VALUES  ( '{0}' , 
                                          '{1}' , 
                                          '{2}' , 
                                          '{3}' ,
                                          GETDATE()
                                        );
                                        SELECT  FAttendUserID ,
                                         FAttendUserType ,
                                         FCard ,
                                         FCardDateTime ,
                                         FGetDateTime ,
                                         FRemark
                                FROM    dbo.xxt_CheckAttends
                                WHERE   FCheckAttendID = @@IDENTITY",
                                                         strOrgID,
                                                         strICCardNum,
                                                         CommonMethod.ConvertStringToDateTime(strCardTime),
                                                         strPhoneSerialNumber);

                    var dataReader = adapter.ExecuteDataReader(strSqlCommand);
                    if (dataReader.Read())
                    {
                        Guid userID = dataReader.GetGuid(0);
                        int intUserType = dataReader.GetInt32(1);

                        attendMsg.CardNO = dataReader.GetString(2);
                        attendMsg.CardTime = dataReader.GetDateTime(3);
                        attendMsg.InputTime = dataReader.GetDateTime(4);
                        if (!dataReader.IsDBNull(5))
                        {
                            attendMsg.Remark = dataReader.GetString(5);
                        }
                        attendMsg.UserName = strUserName;
                        strSqlCommand = string.Empty;
                        if (intUserType == 1)
                        {
                            strSqlCommand = string.Format(@"SELECT  FName
                                      FROM    dbo.Departments
                                      WHERE   FDepartmentID = '{0}'", deptID);
                            strGroupName = string.Format("T{0}", strOrgID);
                        }
                        if (intUserType == 2)
                        {
                            strSqlCommand = string.Format(@"SELECT  gra.FName + cla.FName AS ClassName
                                                    FROM    dbo.Classes AS cla
                                                    LEFT JOIN dbo.Grades AS gra ON cla.FGradeID = gra.FGradeID
                                                    WHERE   cla.FClassID = '{0}'", deptID);
                            strGroupName = string.Format("S{0}", strOrgID);
                        }
                        if (!dataReader.IsClosed)
                        {
                            dataReader.Close();
                        }
                        dataReader = adapter.ExecuteDataReader(strSqlCommand);
                        if (dataReader.Read())
                        {
                            attendMsg.DeptName = dataReader.GetString(0);
                        }
                    }
                }
            }
            return result;
        }
    }
}
